package com.kqb.sgadmin.config;

import com.aliyun.captcha20230305.Client;
import com.aliyun.teaopenapi.models.Config;
import com.kqb.sgadmin.config.properties.CaptchaProperties;
import com.kqb.sgcommon.base.exception.BusinessException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 杉果树皮
 */
@Configuration
@EnableConfigurationProperties(CaptchaProperties.class)
@Slf4j
public class CaptchaConfig {


    @Resource
    private CaptchaProperties captchaProperties;

    @Bean
    public Client captchaClient() {
        Config config = new Config();
        config.setAccessKeyId(captchaProperties.getAccessKeyId());
        config.setAccessKeySecret(captchaProperties.getAccessKeySecret());
        config.setEndpoint(captchaProperties.getEndPoint());

        try {
            return new Client(config);
        } catch (Exception e) {
            log.error("captchaClient 初始化失败{}", e.getMessage());
            throw BusinessException.of(e.getMessage());
        }
    }

}
